﻿
    local zKP_Client_columns = {};
    local wl_list = nil;
    
function zKP_showOrHideWaitingListFrame()
    zKP_initializeBiddingFrame();
    local frame = getglobal("WaitingListFrame");
    
    if (frame:IsVisible() ~= nil) then    
        frame:Hide();
    else
        frame:Show();
    end
end



function zKP_initializeBiddingFrame()
        zKP_initializeBiddingFrameColumns();

        local waitingListFrame = getglobal("WaitingListFrame");
        
        highlight   = { ["r"] = 0.0, ["g"] = 0.0, ["b"] = 0.7, ["a"] = 0.5 };        
        numRows     = 7;
        rowHeight   = 15
        
        if (wl_list == nil) then 
            wl_list = ScrollingTable:CreateST(zKP_Client_columns, numRows, rowHight, highlight, waitingListFrame,"BOTTOMLEFT","BOTTOMLEFT",10,10);            
            wl_list:RegisterEvents({
                                ["OnClick"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, ...)
                                    if (data ~= nil and realrow ~= nil  and cols ~= nil and column ~= nil ) then	
										local celldata = data[realrow].cols[1].value;
										if(CurrentRaid.WaitList[celldata] ~= nil) then
											SendChatMessage("DROP EVERYTHING! YOUR GUILD NEEDS YOU PRONTO!", "WHISPER", nil, DKPMS:IsPlayerOnline(celldata));
										end

                                    end
                                    
                                    return true;
                                    
                                end,
                            });

        end
        
        
        local zKP_client_row_data = {};
        local loopIndex = 1;
        for playerName,value in pairs(CurrentRaid.WaitList) do 

            local onlineStatus = "Offline";
            local onlineOnChar = DKPMS:IsPlayerOnline(playerName);
            local name         = nil;
            local class        = nil;
            
            --If the playing char is an alt, then retrieve the associated mains char name.
            if(AltList[playerName] ~= nil) then
               name = AltList[playerName]; 
            else
               name = playerName;               
            end
                                   
            --If the playing char isnt registrered in the DKP Server then set the class to an empty string value                                 
            if (zUserClasses[name] ~= nil) then
                class = zUserClasses[name];
            else
                class = "NOT REGISTRED";
            end
            
            if (onlineOnChar ~= nil) then
                onlineStatus = "Online";
            else
                onlineStatus = "Offline";
                onlineOnChar = "";
            end
                    
            zKP_client_row_data[loopIndex]             = {}
            zKP_client_row_data[loopIndex].cols        = zKP_fillColumnsWithData(name,
                                                                                 class, 
                                                                                 onlineOnChar, 
                                                                                 onlineStatus);
            zKP_client_row_data[loopIndex].color       = {}
            zKP_client_row_data[loopIndex].colorargs   = {};
            loopIndex = loopIndex + 1 ;
                 
        end
        

        
        wl_list:SetData(zKP_client_row_data);
        

end

function zKP_fillColumnsWithData(player, class, onAlt, onlineStatus)

        local zKP_Client_data = {};
        local zKP_coler_white = { ["r"] = 1.00, ["g"] = 1.00, ["b"] = 1.00, ["a"] = 1.0 };
        local zKP_coler_red = { ["r"] = 1.00, ["g"] = 0.00, ["b"] = 0.00, ["a"] = 1.0 };
        local zKP_coler_green = { ["r"] = 0.00, ["g"] = 1.00, ["b"] = 0.00, ["a"] = 1.0 };
        
        --Player column
        zKP_Client_data[1]                  = {};
            zKP_Client_data[1].value        = player;
            zKP_Client_data[1].args         = nil;
            zKP_Client_data[1].color        = zKP_coler_white;
            zKP_Client_data[1].colorargs    = nil;
        
        --Main Class column
        zKP_Client_data[2]                    = {};
            zKP_Client_data[2].value          = class;
            zKP_Client_data[2].args           = nil;
            zKP_Client_data[2].color          = zKP_determineClassColor(class);
            zKP_Client_data[2].colorargs      = nil;
        
        --Online on Char column
        zKP_Client_data[3]                 = {};
            zKP_Client_data[3].value       = onAlt;
            zKP_Client_data[3].args        = nil;
            zKP_Client_data[3].color       = zKP_coler_white;
            zKP_Client_data[3].colorargs   = nil;
        
        --Online status column
        zKP_Client_data[4]                 = {};
            if (onlineStatus == "Online") then 
                zKP_Client_data[4].value       = onlineStatus;
                zKP_Client_data[4].color       = zKP_coler_green;
            else
                zKP_Client_data[4].value       = onlineStatus;
                zKP_Client_data[4].color       = zKP_coler_red;
            end
            
            zKP_Client_data[4].args        = nil;
            
            zKP_Client_data[4].colorargs   = nil;   
        
        --Last WL Passed column
        zKP_Client_data[5]                 = {};
            
            if (CurrentRaid.WaitListLastPassed[player] ~= nil ) then
                local timeFrame = time() - CurrentRaid.WaitListLastPassed[player];
                if(timeFrame > 600) then --600 equals 10mins
                    zKP_Client_data[5].color       = zKP_coler_red;
                else
                    zKP_Client_data[5].color       = zKP_coler_green;
                end
                zKP_Client_data[5].value       = date("%H:%M:%S",CurrentRaid.WaitListLastPassed[player]);
                
            else
                zKP_Client_data[5].value       = "Joined < 10min";
                zKP_Client_data[5].color       = zKP_coler_green;
            end
            
            zKP_Client_data[5].args        = nil;
            zKP_Client_data[5].colorargs   = nil;   
        
        return zKP_Client_data;
end    

function zKP_initializeBiddingFrameColumns()
        zKP_Client_columns =  {
                    {
                        ["name"] = "Player",
                        ["width"] = 100,
                        ["align"] = "LEFT",
                        ["bgcolor"] = { ["r"] = 0.0, ["g"] = 0.0, ["b"] = 0.0, ["a"] = 0.9 },
                    }, -- [1]
                    { 
                        ["name"] = "Main Class", 
                        ["width"] = 90, 
                        ["align"] = "CENTER",
                        ["bgcolor"] = { ["r"] = 0.0, ["g"] = 0.0, ["b"] = 0.0, ["a"] = 0.4 },
                    }, -- [2]
                    { 
                        ["name"] = "Online on Char", 
                        ["width"] = 110, 
                        ["align"] = "CENTER",
                        ["bgcolor"] = { ["r"] = 0.0, ["g"] = 0.0, ["b"] = 0.0, ["a"] = 0.9 },
                    }, -- [3]
                    { 
                        ["name"] = "Online Status", 
                        ["width"] = 90, 
                        ["align"] = "CENTER",
                        ["bgcolor"] = { ["r"] = 0.0, ["g"] = 0.0, ["b"] = 0.0, ["a"] = 0.4 },
                    }, -- [4]                    
                    { 
                        ["name"] = "Last WL Passed", 
                        ["width"] = 90, 
                        ["align"] = "CENTER",
                        ["bgcolor"] = { ["r"] = 0.0, ["g"] = 0.0, ["b"] = 0.0, ["a"] = 0.9 },
                    }, -- [5]
                };
end

--[[ This function determines which color code should be used depending on which class the sender is.
 The information about the senders class is retrieved from the global variable defined in DKPManager client.toc file. ]]
function zKP_determineClassColor(class)
    
    
    class = string.lower(class);

    if    (class == "shaman") then
        return { ["r"] = 0.14, ["g"] = 0.35, ["b"] = 1.0, ["a"] = 1.0 };    --SHAMAN 	        0.14, 0.35, 1.0, 1.0    - DARK BLUE
    elseif(class == "paladin") then
        return { ["r"] = 0.96, ["g"] = 0.55, ["b"] = 0.73, ["a"] = 1.0 };   --PALADIN           0.96, 0.55, 0.73, 1.0   - PINK
    elseif(class == "priest") then  
        return { ["r"] = 1.00, ["g"] = 1.00, ["b"] = 1.00, ["a"] = 1.0 };   --PRIEST 	        1.0 , 1.0 , 1.0, 1.0    - WHITE
    elseif(class == "rogue") then
        return { ["r"] = 1.00, ["g"] = 0.96, ["b"] = 0.41, ["a"] = 1.0 };   --ROGUE 	        1.0 , 0.96, 0.41, 1.0   - YELLOW
    elseif(class == "warrior") then
        return { ["r"] = 0.78, ["g"] = 0.61, ["b"] = 0.43, ["a"] = 1.0 };   --WARRIOR 		    0.78, 0.61, 0.43, 1.0   - BROWN
    elseif(class == "druid") then
        return { ["r"] = 1.00, ["g"] = 0.49, ["b"] = 0.04, ["a"] = 1.0 };   --DRUID 	        1.0 , 0.49, 0.04, 1.0   - ORANGE
    elseif(class == "hunter") then
        return { ["r"] = 0.67, ["g"] = 0.83, ["b"] = 0.45, ["a"] = 1.0 };   --HUNTER 	        0.67, 0.83, 0.45, 1.0   - GREEN
    elseif(class == "mage") then
        return { ["r"] = 0.41, ["g"] = 0.8 , ["b"] = 0.94, ["a"] = 1.0 };   --MAGE 	            0.41, 0.8 , 0.94, 1.0   - LIGHT BLUE
    elseif(class == "warlock") then
        return { ["r"] = 0.58, ["g"] = 0.51, ["b"] = 0.79, ["a"] = 1.0 };   --WARLOCK 	        0.58, 0.51, 0.79, 1.0   - PURPLE
    elseif(class == "death knight") then
        return { ["r"] = 0.77, ["g"] = 0.12, ["b"] = 0.23, ["a"] = 1.0 };   --DEATH KNIGHT 	    0.77, 0.12, 0.23, 1.0   - DARK RED
    else
        return { ["r"] = 1.00, ["g"] = 1.00, ["b"] = 1.00, ["a"] = 1.0 };   --RETURNS WHITE WHEN SENDERS CLASS DOSNT FIT THE IF CLAUSES
    end  
end






